const mongoose = require("mongoose");


//新增用户购物车的数据
module.exports.addUserCart = async (userCart) => {
  return await mongoose.model("userCartModel").create(userCart);
}
//获得用户购物车的数据
module.exports.getUserCart = async ({userId,isjiesuan}) => {
  return await mongoose.model("userCartModel").find({userId:userId,isjiesuan:isjiesuan}).populate("goodId");
}
//更新用户购物车的数据 把购物
module.exports.updataUserCart = async ({_id,count}) => {
  return await mongoose.model("userCartModel").updateOne({_id:_id},{isjiesuan:true,count:count});
}
//删除用户购物车的数据
module.exports.deleteUserCart = async ({_id}) => {
  return await mongoose.model("userCartModel").deleteOne({_id:_id})
}

//更新订单
module.exports.updataOrder = async ({_id,state}) => {
  return await mongoose.model("ordersModel").updateOne({ _id },{state});
}
//新增订单
module.exports.addOrders = async newOrders => {
  const { _id } = await mongoose.model("orderGoodsModel").create(newOrders);
  const now = new Date()
  let month = now.getMonth() + 1
  let day = now.getDate()
  let hour = now.getHours()
  let minutes = now.getMinutes()
  let seconds = now.getSeconds()
  let name= now.getFullYear().toString() + month.toString() + day + hour + (Math.round(Math.random() * 23 + 100)).toString()
  return await mongoose.model("ordersModel").create({ ...newOrders,orderGoodsId: _id,name:name})
  return true;
}

module.exports.getGoods = async () => {
  return await mongoose.model("goodsModel").find();
}
module.exports.getmendian = async (shopId) => {
  return await mongoose.model("shopsModel").find({_id:shopId,status:2})
}
//获取所有订单 待付款1/待收货2/待评价3
module.exports.getOrders = async ({
  currentPage = 1,
  pageSize = 5,
  userId = '',
  state=1,
}) => {
  console.log(userId)
  const tableData = {
    currentPage: ~~currentPage,
    pageSize: ~~pageSize,
    userId: userId,
    state:~~state,
  }
  try {
    if (userId === "") {
      tableData.totalCount = await mongoose.model("ordersModel").find({state:state}).count();
      tableData.orders = await mongoose.model("ordersModel").find({state:state})
        .skip((tableData.currentPage - 1) * tableData.pageSize)
        .limit(tableData.pageSize)
        .populate("userId")
        .populate({
          path: "orderGoodsId",
          populate: {
            path: 'goodId'
          }
        })
      return tableData;
    }
    tableData.totalCount = await mongoose.model("ordersModel").find({userId:userId,state:state}).count();
    tableData.orders = await mongoose.model("ordersModel").find({
        userId:userId,
        state:state
      })
      .skip((tableData.currentPage - 1) * tableData.pageSize)
      .limit(tableData.pageSize)
      .populate("userId")
      .populate({
        path: "orderGoodsId",
        populate: {
          path: 'goodId'
        }
      })
    return tableData;
  } catch (e) {
    console.log('=============== getMovies 异常: =====================');
    console.log(e);
    console.log('====================================');
  }
}

//通过id获取订单
module.exports.getOrderById = async _id => {
  return await mongoose.model("ordersModel").find(_id)
    .populate("userId")
    .populate("orderGoodsId")
}
//通过id删除订单
module.exports.deleteOrdersById = async OrdersId => {
  const {
    orderGoodsId
  } = await mongoose.model("ordersModel").find({
    _id: OrdersId
  });
  const aaa = await mongoose.model("orderGoodsModel").deleteOne(orderGoodsId);
  return await mongoose.model("ordersModel").deleteOne(OrdersId);
}